/**********************************************
*  Classes:   ParticleSystem
*  Desc:      Abstract class that manages creation,
*			  removal and update of particles
*  Author:    Vaios Kalpias-Ilias (C) 2008-2009
***********************************************/

#include "Particle.h"
#include "ParticleSystem.h"

//---------------------------------------------------
mth::Vector3 ParticleSystem::Gravity(0.0f, -9.8f, 0.0f);
//---------------------------------------------------
/**
* Ctor
*/
ParticleSystem::ParticleSystem()
: m_iParticlesPerTick(0),
  m_iRate(0),
  m_iParticleLife(0),
  m_iSystemLife(0)
{
}
//---------------------------------------------------
/**
* Dtor
*/
ParticleSystem::~ParticleSystem()
{
	
}
//---------------------------------------------------
/**
* Resets system
*/
void ParticleSystem::Reset()
{
	

}
//-------------------------------------------------
/**
* Integrates particle positions / velocities
*/
void ParticleSystem::MoveParticles(float dt)
{
	std::list<SceneNodePtr>::iterator it;
	for (it = m_lChildren.begin();
		it != m_lChildren.end();
		++it)
	{
		if ((*it)->GetType() == NODE_PARTICLE)
		{
			ParticlePtr p =
				boost::static_pointer_cast<Particle>(*it);
			p->SetTranslation(p->GetTranslation() + p->GetVelocity() * dt);
			p->SetVelocity(p->GetVelocity() + Gravity*dt);
		}
	}
}
//-------------------------------------------------
/**
* Pre update step
*/
void ParticleSystem::PreUpdate()
{
	TransformNode::PreUpdate();

	m_iSystemLife--;
	if (m_iSystemLife <= 0)
	{
		return;
	}

	// create particles every m_iRate
	m_iRate--;
	if (m_iRate <= 0)
	{
		CreateParticles();
		
	}

}
//-------------------------------------------------
/**
* Post update step
*/
void ParticleSystem::PostUpdate()
{
	TransformNode::PostUpdate();	
}


